package com.majie.algorithm.linked_list;

public class CircleListMain {
    /**
     * 约瑟夫环
     */
    public static int cir(int n, int m) {
        int p = 0;
        for (int i = 2; i <= n; i++) {
            p = (p + m) % i;
        }
        return p + 1;
    }
    /**
     * 约瑟夫环(循环链表解决)
     */
    public static CircleList<Integer> circle(int n, int m) {
        CircleList<Integer> list = new CircleList<>();
        for (int i = 1; i <= n; i++) {
            list.add(i);
        }
        System.out.println(list);

        list.reset();

        while (list.size > 1) {
            for (int i = 0; i < m - 1; i++) {
                list.next();
            }
            list.remove();
        }
        return list;
    }
    public static void main(String[] args) {
        int n = 111;
        int m = 30;
        int cir = cir(n, m);
        System.out.println(cir);
        CircleList<Integer> circle = circle(n, m);
        System.out.println(circle);
    }
}
